package com.example.literaturesystem.controller.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.literaturesystem.domain.UserUpdate;
import com.example.literaturesystem.domain.UserTeam;
import com.example.literaturesystem.domain.team.Team;
import com.example.literaturesystem.domain.user.User;
import com.example.literaturesystem.service.team.ITeamService;
import com.example.literaturesystem.service.user.IUserService;
import com.example.literaturesystem.util.ValidatorUtils;
import com.google.gson.Gson;

@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	private IUserService userService;
	@Autowired
	private ITeamService teamService;

	@RequestMapping(method = RequestMethod.GET)
	public String get() {
		User user = new User();
		user.setIfuse("y");
		List<User> userList = this.userService.query(user);
		Gson gson = new Gson();
		return gson.toJson(userList);
	}

	@RequestMapping(value = "/queryUser/{param}/{value}", method = RequestMethod.GET)
	public String queryUser(@PathVariable("param") String param, @PathVariable("value") String value) {
		User user = new User();
		user.setIfuse("y");
		switch (param) {
		case "userTeamNo": {
			user.setUserTeamNo(value);
			break;
		}
		case "userNo": {
			user.setUserNo(value);
			break;
		}
		}
		List<User> userList = this.userService.query(user);
		Team t = new Team();
		t.setTeamNo(userList.get(0).getUserTeamNo());
		System.out.println(t.getTeamNo());
		List<Team> teamList = this.teamService.query(t);
		Gson gson = new Gson();
		switch (param) {
		case "userTeamNo": {
			user.setUserTeamNo(value);
			return gson.toJson(userList);
		}
		case "userNo": {
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("user", userList.get(0));
			resultMap.put("team", teamList.get(0));
			return gson.toJson(resultMap);
		}
		}
		/*
		 * int flag = userList.size();
		 * 
		 * Map<String, Object> resultMap = new HashMap(); if (flag > -1) {
		 * resultMap.put("code", "0000"); resultMap.put("data", userList); }
		 * else { resultMap.put("code", "9999"); resultMap.put("data", "error");
		 * }
		 */
		return value;
	}

	/*
	 * @RequestMapping(value = "/query/{UserNo}/{Password}", method =
	 * RequestMethod.GET) public String query(@PathVariable("UserNo") String
	 * UserNo, @PathVariable("Password") String Password) { User user = new
	 * User(); user.setIfuse("y"); user.setUserNo(UserNo);
	 * user.setPassword(Password); List<User> userList =
	 * this.userService.query(user); Map<String, Object> resultMap = new
	 * HashMap(); if (userList.size() > 0) { resultMap.put("code", "0000");
	 * resultMap.put("data", userList.get(0)); } else { resultMap.put("code",
	 * "9999"); resultMap.put("data", "error"); } Gson gson = new Gson(); return
	 * gson.toJson(resultMap); }
	 */
	@RequestMapping(value = "/query/{UserNo}/{Password}", method = RequestMethod.GET)
	public String query(@PathVariable("UserNo") String UserNo, @PathVariable("Password") String Password) {
		User user = new User();
		user.setIfuse("y");
		if (ValidatorUtils.isMobile(UserNo)) {
			user.setUserPhone(UserNo);
		} else if (ValidatorUtils.isEmail(UserNo)) {
			user.setUserEmail(UserNo);
		} else {
			user.setUserNo(UserNo);
			System.out.println("d");
		}
		System.out.println(UserNo);
		user.setPassword(Password);
		System.out.println(Password);
		List<User> userList = this.userService.query(user);
		System.out.println("d");
		Map<String, Object> resultMap = new HashMap();
		System.out.println("d");
		if (userList.size() > 0) {
			resultMap.put("code", "0000");
			resultMap.put("data", userList.get(0));
		} else {
			System.out.println(Password);
			resultMap.put("code", "9999");
			resultMap.put("data", "error");
		}
		Gson gson = new Gson();
		return gson.toJson(resultMap);
	}

	@RequestMapping(method = RequestMethod.PUT)
	public String update(@RequestBody User user) {
		Map<String, String> resultMap = new HashMap();
		if (!StringUtils.isEmpty(user.getUserNo())) {
			int flag = this.userService.update(user);
			if (flag == 1) {
				resultMap.put("code", "0000");
				resultMap.put("data", "success");
			}
		} else {
			resultMap.put("code", "9999");
			resultMap.put("data", "error");
		}
		Gson gson = new Gson();
		return gson.toJson(resultMap);
	}

	@RequestMapping(method = RequestMethod.POST)
	public String add(@RequestBody User user) {
		user.setIfuse("y");
		user.setPassword(user.getUserNo());
		// 通知信息添加至数据库
		int flag = this.userService.insert(user);
		Map<String, String> resultMap = new HashMap();
		if (flag == 1) {
			resultMap.put("code", "0000");
			resultMap.put("data", "success");
		} else {
			resultMap.put("data", "error");
			resultMap.put("code", "9999");
		}
		Gson gson = new Gson();
		return gson.toJson(resultMap);
	}

	@RequestMapping(method = RequestMethod.POST, value = "insert")
	public String insert(@RequestBody UserTeam userTeam) {
		User user = new User();
		user.setIfuse("n");
		user.setUserNo(userTeam.getUserNo());
		user.setUserTeamNo(userTeam.getUserTeamNo());
		user.setUserName(userTeam.getUserName());
		user.setPassword(userTeam.getPassword());
		user.setUserPhone(userTeam.getUserPhone());
		user.setUserEmail(userTeam.getUserEmail());
		Team team = new Team();
		team.setIfuse("n");
		team.setTeamNo(userTeam.getTeamNo());
		team.setTeamName(userTeam.getTeamName());
		team.setTeamEmail(userTeam.getTeamEmail());
		team.setMaxNum(userTeam.getMaxNum());
		team.setUserName(userTeam.getUserName());
		team.setUserEmail(userTeam.getUserEmail());
		int flag = this.teamService.insert(team);
		int flag1 = this.userService.insert(user);
		Map<String, String> resultMap = new HashMap();
		if (flag == 1 && flag1 == 1) {
			resultMap.put("code", "0000");
			resultMap.put("data", "success");
		} else {
			resultMap.put("data", "error");
			resultMap.put("code", "9999");
		}
		Gson gson = new Gson();
		return gson.toJson(resultMap);
	}

	@RequestMapping(method = RequestMethod.POST, value = "addteamuser") // 团队管理员添加成员
	public String addteamuser(@RequestBody User user) {
		user.setIfuse("y");
		int flag1 = this.userService.insert(user);
		Map<String, String> resultMap = new HashMap();
		if (flag1 == 1) {
			resultMap.put("code", "0000");
			resultMap.put("data", "success");
		} else {
			resultMap.put("data", "error");
			resultMap.put("code", "9999");
		}
		Gson gson = new Gson();
		return gson.toJson(resultMap);
	}

	@RequestMapping(method = RequestMethod.POST, value = "update")
	public String insert(@RequestBody UserUpdate userUpdate) {
		User user = new User();
		/* user.setIfuse("n"); */
		user.setUserNo(userUpdate.getUserNo());
		user.setUserTeamNo(userUpdate.getUserTeamNo());
		user.setUserName(userUpdate.getUserName());
		user.setPassword(userUpdate.getPassword());
		user.setUserPhone(userUpdate.getUserPhone());
		user.setUserEmail(userUpdate.getUserEmail());
		user.setiDNo(userUpdate.getiDNo());
		user.setNote(userUpdate.getNote());
		Team team = new Team();
		team.setTeamNo(userUpdate.getTeamNo());
		team.setTeamName(userUpdate.getTeamName());
		team.setUserName(userUpdate.getUserName());
		team.setUserEmail(userUpdate.getUserEmail());
		int flag = this.teamService.update(team);
		int flag1 = this.userService.update(user);
		Map<String, String> resultMap = new HashMap();
		if (flag == 1 && flag1 == 1) {
			resultMap.put("code", "0000");
			resultMap.put("data", "success");
		} else {
			resultMap.put("data", "error");
			resultMap.put("code", "9999");
		}
		Gson gson = new Gson();
		return gson.toJson(resultMap);
	}
}
